package ensembl_parameters;

require Exporter;


our @ISA = qw(Exporter);
our $VERSION = 1.00;
our @EXPORT = qw(   get_ensembl_taxon_data
					get_ensmart_ftp_directory_name
					get_enscompara_ftp_directory_name
					get_ensmart_db_name
					get_enscompara_db_name
					get_enscore_db_name
					get_ens_taxon_mysql_core_ftp_directory_name
					ENS_TAXON 
					ENS_VERSION 
					ALT_SOURCE 
					ENS_COMMONTAXON 
					ENS_FTP_SQL_DIR
					ENS_CORE_DB_NAME
					ENS_FTP_FASTA_DIR
					ENS_TAXON_ABBREV
					$ens_db_host
					$ensembl_root 
					$ens_db_user 
					$ens_compara_version  
					$ens_mart_version
					);


use FindBin;
use lib ("$FindBin::Bin/..", "/net/cpp-group/Leo/bin");
use db_parameters;
use strict;
use warnings;
use DBI;
use DBD::Pg;
use Carp;
use constant ENS_TAXON		 	=> 1;
use constant ENS_VERSION	 	=> 2;
use constant ALT_SOURCE      	=> 3;
use constant ENS_MART        	=> 3;
use constant ENS_COMMONTAXON 	=> 4;
use constant ENS_TAXON_ABBREV	=> 5;
use constant ENS_FTP_SQL_DIR 	=> 6; 
use constant ENS_CORE_DB_NAME 	=> 7; 
use constant ENS_FTP_FASTA_DIR  => 8; 


use FindBin;
our $ensembl_root;
our $ens_db_host;
our $ens_db_user;
our $ens_compara_version;
our $ens_mart_version;





BEGIN

#
#	Get parameters for entire database
#
#	These are found in the DB_PARAMETERS file in the parent directory of the running script
#
{
	my $file_name = "$FindBin::Bin/../DB_PARAMETERS";
	if ($ENV{GENOME_DB_PARAM_DIR})
	{
		$file_name = $ENV{GENOME_DB_PARAM_DIR};
		$file_name =~ s/^(.*)\/?$/$1\/DB_PARAMETERS/;
	}
	open DB_PARAMETERS, $file_name or
	    croak "Error\n\tCould not open $FindBin::Bin/../DB_PARAMETERS\n$@";
	my %params;
	while (<DB_PARAMETERS>)
	{
		chomp;
		next unless m/^(.*?)[ \t]*=[ \t]*(.*?)[ \t]*$/;
		$params{uc $1} = $2;
	}

	$ens_db_host			= $params{ENS_DB_HOST};
	$ens_db_user			= $params{ENS_DB_USER};
	$ens_compara_version	= $params{ENS_COMPARA_VERSION};
	$ens_mart_version		= $params{ENS_MART_VERSION};
}















#-----------------------------------------------------------------------------------------
#	Get per taxon mysql data directory name
#
#-----------------------------------------------------------------------------------------
sub get_ens_taxon_directory_name
{
	my ($taxon, $opt_ens_version) = @_;
	
	#ftp://ftp.ensembl.org/pub/release-27/human-27.35a/data/mysql
	#ftp://ftp.ensembl.org/pub/release-34/human-34.35g/data/mysql/
	#ftp://ftp.ensembl.org/pub/release-35/homo_sapiens_35_35h/data/mysql/
	if (!defined $opt_ens_version)
	{
		$opt_ens_version = $taxon->[ENS_VERSION];
	}


	#
	#	Release version
	#
	# 		release-34
	my $ens_major_version = $opt_ens_version;
	$ens_major_version =~ s/_.*//;
	my $dir_name = "ftp://ftp.ensembl.org/pub/release-" .$ens_major_version;


	#
	#	base directory
	#
	#		canis_familiaris_core_36_1e
	my $ens_ftp_dir_name;
	if ($ens_major_version >= 35)
	{
		$ens_ftp_dir_name = $taxon->[ENS_TAXON] . "_" . $opt_ens_version;
		$ens_ftp_dir_name =~ tr/./_/;
	}
	else
	{
		my $version = $opt_ens_version;
		$version =~ tr/_/./;
		$ens_ftp_dir_name = $taxon->[ENS_COMMONTAXON] . "-" . $version;
	}


	#
	#	put it together
	#
	return $dir_name . '/'.$ens_ftp_dir_name.'/data';
}

sub get_ens_taxon_sequence_directory_name
{
	my ($taxon, $opt_ens_version) = @_;
	return get_ens_taxon_directory_name($taxon, $opt_ens_version).'/fasta';
}

sub get_enscore_db_name
{
	#
	#	db name
	#
	#		canis_familiaris_core_36_1e
	my ($taxon, $opt_ens_version) = @_;

	#ftp://ftp.ensembl.org/pub/release-27/human-27.35a/data/mysql
	#ftp://ftp.ensembl.org/pub/release-34/human-34.35g/data/mysql/
	#ftp://ftp.ensembl.org/pub/release-35/homo_sapiens_35_35h/data/mysql/
	if (!defined $opt_ens_version)
	{
		$opt_ens_version = $taxon->[ENS_VERSION];
	}

	return $taxon->[ENS_TAXON] .'_core_'.$opt_ens_version;
}
sub get_ens_taxon_mysql_core_ftp_directory_name
{
	my ($taxon, $opt_ens_version) = @_;
	
	return get_ens_taxon_directory_name($taxon, $opt_ens_version).'/mysql/'.get_enscore_db_name($taxon, $opt_ens_version);
}



#-----------------------------------------------------------------------------------------
#	Get data per taxa. Basically the above indexed by taxon name
#
#-----------------------------------------------------------------------------------------
sub get_ensembl_taxon_data()
{
	#
	#	Get DB parameters
	#

	$db_host && $db_name && $db_user
	 or croak "Error:\nDatabase parameters corrupted.\n".
			"Check for the [DB_PARAMETERS] file in the project root.\n";

	@_[1,2,3] = ($db_host, $db_name, $db_user);
	# connect to genome database
	my $dbh = DBI->connect( "dbi:Pg:dbname=$db_name;host=$db_host",
							$db_user,
							'',
							{
							  RaiseError => 1,
							  AutoCommit => 0,
							  PrintError => 1
							}
						  )
			or croak "Database connection not made: $DBI::errstr";



	my $array = $dbh->selectall_arrayref(
				"SELECT species, ens_taxon, ens_version, ens_mart, ".
						"ens_commonname, ens_taxon_abbrev ".
					"FROM genomes.ensembl_parameters;")
			 or croak DBI::errstr();
	$dbh->disconnect();

	my %taxa_data;
	for my $per_taxon(@$array)
	{
		my $ftp_dir_version = $per_taxon->[ENS_VERSION];
		$ftp_dir_version =~ s/_/\./;
		$per_taxon->[ENS_FTP_SQL_DIR]= get_ens_taxon_mysql_core_ftp_directory_name($per_taxon);
		$per_taxon->[ENS_FTP_FASTA_DIR]= get_ens_taxon_sequence_directory_name($per_taxon);
	}
	croak unless @$array;
	return @$array;
}




sub get_ensmart_db_name
{
	#ftp://ftp.ensembl.org/pub/release-27/mart-27.1/data/mysql/ensembl_mart_27_1/
	#ftp://ftp.ensembl.org/pub/release-36/mart_36/data/mysql/ensembl_mart_36/
	my ($opt_ens_version) = @_;
	if (!defined $opt_ens_version)
	{
		$opt_ens_version = $ens_mart_version;
	}

	my $ens_db_name = 'ensembl_mart_'.$opt_ens_version;
	$ens_db_name =~ tr/./_/;
	return $ens_db_name;
}

sub get_ensmart_ftp_directory_name
{
	my ($opt_ens_version) = @_;
	if (!defined $opt_ens_version)
	{
		$opt_ens_version = $ens_mart_version;
	}
	my $ens_major_version = $opt_ens_version;

	$ens_major_version =~ s/\_.*//;


	#
	#	ftp directory
	#
	my $ens_db_dir = "ftp://ftp.ensembl.org/pub/release-$ens_major_version";
	#switched from hyphens to underscores
	if ($ens_major_version < 35)
	{
		my $version = $opt_ens_version;
		$version =~ tr/_/./;
		$ens_db_dir .= "/mart-$version";
	}
	else
	{
		$ens_db_dir .= "/mart_$opt_ens_version";
	}

	$ens_db_dir.= '/data/mysql/'.get_ensmart_db_name($opt_ens_version);
	return $ens_db_dir;
}

sub get_enscompara_db_name
{
	my ($opt_ens_version) = @_;
	if (!defined $opt_ens_version)
	{
		$opt_ens_version = $ens_compara_version;
	}

	#ftp://ftp.ensembl.org/pub/release-27/multi-species-27.1/data/mysql/ensembl_compara_27_1";
	my $ens_db_name = 'ensembl_compara_'.$opt_ens_version;
	$ens_db_name =~ tr/./_/;
	return $ens_db_name;
}

sub get_enscompara_ftp_directory_name
{
	my ($opt_ens_version) = @_;
	if (!defined $opt_ens_version)
	{
		$opt_ens_version = $ens_compara_version;
	}

	my $ens_major_version = $opt_ens_version;
	$ens_major_version =~ s/\_.*//;


	#
	#	ftp directory
	#
	my $ens_db_dir = "ftp://ftp.ensembl.org/pub/release-$ens_major_version";
	#switched from hyphens to underscores
	if ($ens_major_version < 35)
	{
		my $version = $opt_ens_version;
		$version =~ tr/_/./;
		$ens_db_dir .= "/multi-species-$version";
	}
	else
	{
		#ftp://ftp.ensembl.org/pub/release-36/multi-species_36/data/mysql/ensembl_compara_35";
		#ftp://ftp.ensembl.org/pub/release-34/multi-species-34/data/mysql/ensembl_compara_34";
		$ens_db_dir .= "/multi_species_$opt_ens_version";
	}

	$ens_db_dir.= '/data/mysql/'.get_enscompara_db_name($opt_ens_version);
	return $ens_db_dir;
}







return 1;
